import { reactive, ref } from 'vue'
import { storageManager } from '@/utils/storage'

export function useNews(){
  // 频道数据
  const newsChannels = reactive([
    { id: 'recommend', name: '推荐', hasNew: true },
    { id: 'hot', name: '热点', hasNew: false },
    { id: 'health', name: '健康', hasNew: false },
    { id: 'community', name: '社区', hasNew: true }
  ])
  
  // 当前频道
  const currentChannel = ref('recommend')
  
  // 新闻列表
  const newsList = reactive([
    { 
      id: 'n1', 
      title: '社区养老服务新举措，让老年生活更美好', 
      icon: '/static/parcel.svg',
      image: 'https://via.placeholder.com/100',
      source: '社区通知',
      time: '1小时前',
      read: false,
      isNew: true
    },
    { 
      id: 'n2', 
      title: '老年大学秋季招生开始啦，多门课程等你来', 
      icon: '/static/ticket.svg',
      image: 'https://via.placeholder.com/100',
      source: '教育资讯',
      time: '2小时前',
      read: false,
      isNew: true
    },
    { 
      id: 'n3', 
      title: '医保报销流程简化指南，一看就会', 
      icon: '/static/food.svg',
      image: 'https://via.placeholder.com/100',
      source: '健康医疗',
      time: '3小时前',
      read: false,
      isNew: false
    }
  ])
  
  // 置顶新闻
  const topNews = reactive([
    {
      id: 't1',
      title: '下午3点用药提醒 - 请准时服用降压药',
      image: 'https://via.placeholder.com/100',
      source: '健康助手',
      time: '刚刚'
    }
  ])
  
  // 加载状态
  const newsLoading = ref(false)
  const newsRefreshing = ref(false)
  const newsNoMore = ref(false)
  
  // 分页参数
  const page = ref(1)
  const pageSize = ref(10)
  
  // 加载新闻数据
  const loadNewsData = async () => {
    newsLoading.value = true
    
    return new Promise((resolve) => {
      setTimeout(() => {
        console.log('新闻数据加载完成')
        newsLoading.value = false
        resolve(true)
      }, 500)
    })
  }
  
  // 切换频道
  const switchChannel = (channelId) => {
    currentChannel.value = channelId
    page.value = 1
    newsNoMore.value = false
    newsList.length = 0
    
    // 重新加载该频道的新闻
    loadNewsData()
  }
  
  // 下拉刷新
  const refreshNews = async () => {
    newsRefreshing.value = true
    page.value = 1
    newsNoMore.value = false
    
    return new Promise((resolve) => {
      setTimeout(() => {
        // 模拟刷新数据
        console.log('新闻刷新完成')
        newsRefreshing.value = false
        
        // 触觉反馈
        if(uni.vibrateShort){
          try { uni.vibrateShort({ type: 'light' }) } catch(e){}
        }
        
        resolve(true)
      }, 1000)
    })
  }
  
  // 加载更多
  const loadMoreNews = async () => {
    if(newsLoading.value || newsNoMore.value) return
    
    newsLoading.value = true
    page.value += 1
    
    return new Promise((resolve) => {
      setTimeout(() => {
        // 模拟加载更多数据
        if(page.value >= 3){
          newsNoMore.value = true
        }
        
        console.log(`加载第${page.value}页新闻`)
        newsLoading.value = false
        resolve(true)
      }, 800)
    })
  }
  
  // 标记新闻为已读
  const markAsRead = (newsId) => {
    const news = newsList.find(n => n.id === newsId)
    if(news){
      news.read = true
      storageManager.addReadHistory(news)
    }
  }
  
  return { 
    newsChannels, 
    currentChannel, 
    newsList, 
    topNews, 
    newsLoading, 
    newsRefreshing, 
    newsNoMore,
    page,
    pageSize,
    loadNewsData, 
    switchChannel, 
    refreshNews, 
    loadMoreNews,
    markAsRead
  }
}

